package com.loong.seller.handler;

import com.google.common.collect.Lists;
import com.loong.common.domain.Pager;
import com.loong.common.executor.BatchExecutor;
import com.loong.common.rest.rpc.RPCResponse;
import com.loong.common.utils.TransformUtils;
import com.loong.data.dto.ShopDTOResult;
import com.loong.data.param.DistanceAndShopIdListRPCParam;
import com.loong.data.service.IShopService;
import com.loong.seller.business.ShopBusiness;
import com.loong.seller.business.UserShopCollectionBusiness;
import com.loong.seller.common.forehead.param.UserShopCollectionPageQueryParam;
import com.loong.seller.common.forehead.param.UserShopCollectionParam;
import com.loong.seller.common.forehead.param.UserShopCollectionQueryParam;
import com.loong.seller.common.forehead.result.ShopForeheadResult;
import com.loong.seller.model.Shop;
import com.loong.seller.model.UserShopCollection;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Author: hzp
 * @Date: 2021-1-6 15:08
 */
@Component
@Slf4j
public class UserShopCollectionHandler {
    @Resource
    private UserShopCollectionBusiness userShopCollectionBusiness;

    @Resource
    private ShopHandler shopHandler;

    @Resource
    private ShopBusiness shopBusiness;

    @Resource
    private BatchExecutor batchExecutor;

    @DubboReference
    private IShopService iShopService;



    /**
     * 保存收藏
     * @param param 用户店铺信息
     */
    public void save(UserShopCollectionParam param) {
        userShopCollectionBusiness.save(TransformUtils.transform(param, UserShopCollection.class));
    }

    /**
     * 取消收藏
     * @param userId 用户id
     * @param shopIds 店铺id 集合
     */
    public void delete(Long userId,List<Long> shopIds) {
        Condition condition = new Condition(UserShopCollection.class);
        Example.Criteria criteria = condition.createCriteria();
        //根据用户id、店铺id删除用户收藏
        criteria.andEqualTo("userId", userId);
        criteria.andIn("shopId", shopIds);
        userShopCollectionBusiness.deleteByCondition(condition);
    }

    /**
     *
     * @param param param
     * @return
     */
    public Pager<ShopForeheadResult> pageQuery(UserShopCollectionPageQueryParam param, Long userId) {
        Pager<ShopForeheadResult> shopPager = TransformUtils.transformPage(shopBusiness.pageQuery(param, () -> shopHandler.findUserShopCollectionByUserId(userId)), ShopForeheadResult.class);
        List<ShopForeheadResult> dataList = shopPager.getDataList();
        //RPC
        List<Long> shopIdList = Lists.newArrayList();
        if(dataList.size()>0){
            dataList.forEach(shop -> shopIdList.add(shop.getId()));
            //RPC 请求数据
            DistanceAndShopIdListRPCParam buildParam = DistanceAndShopIdListRPCParam.builder().longitude(param.getLongitude()).latitude(param.getLatitude()).shopIdList(shopIdList).build();
            RPCResponse<List<ShopDTOResult>> shopByIdList = iShopService.findShopByIdList(buildParam);
            List<ShopDTOResult> data = shopByIdList.getData();
            List<ShopForeheadResult> shopForeheadResults = TransformUtils.transformList(data, ShopForeheadResult.class);
            shopPager.setDataList(shopForeheadResults);
        }
        return shopPager;
    }

    public Boolean findOne(Long userId, Long shopId) {
        Condition condition = new Condition(UserShopCollection.class);
        Example.Criteria criteria = condition.createCriteria();
        //根据用户id、购物车id删除购物车
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("shopId", shopId);
        return userShopCollectionBusiness.findByCondition(condition).size()>0;
    }

    /**
     *
     * @param idList
     */
    public void batchDelete(List<Long> idList) {
        batchExecutor.batchExecute(idList,id->id.toString(),id->{
            userShopCollectionBusiness.deleteById(id);
        });
    }
}
